/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
var send_flag = true;
$(function(){
    $("body").find("#third-vilid-form").validate({
        submitHandler: function(form) {
            $("#sub-third-btn").attr("disabled","disabled").css("cursor","not-allowed").val("正在绑定中...");
            var data = $(form).serialize();
            doRequest("/account/third_bind",data,false,function(B){
                window.location.href = B.url;
            },function(B){
                layer.msg(B.errorMsg,{time:2000,offset:300,icon:0});
                $("#sub-third-btn").removeAttr("disabled").css("cursor","pointer").val("重新绑定");
            });
        },
        rules:{
            uMobile:{isMobile:true},
            uCode:{smscode:true}
        },		
        messages:{
            uMobile:{required:"请输入绑定的手机号"},
            uCode:{required:"请输入短信验证码"}
        },
        errorPlacement: function(error,element)
        {
            error.appendTo ( element.parent() );
            error.addClass('error');
        },	
        success: function(label) {
            label.remove();
        }
    });
    $(".login-con .p20 ul li").on("click",function(){
        $("input",this).css("border","1px solid #1c273d");
        $(this).siblings().find("input").css("border-color","#e0e0e0");
    });
});
var total_time = 60;
var isMobile = /^(((13[0-9]{1})|(14[0-9]{1})|(15[0-3,5-9]{1})|(17[0,1,3,5-8]{1})|(18[0-9]{1}))+\d{8})$/;

function sendCode(obj){
    var _self = $(obj);
    if(!send_flag){
        layer.msg("操作过于频繁",{time:2000,offset:300,icon:0});return;
    }

    var start_time = function(){
        for (var i = 1; i <= total_time; i++) {
            window.setTimeout("update_p("+ i + "," + total_time + ")", i * 1000);
        }
    }
    var send = function(){
        var mobile = $("#uMobile").val();
        if(mobile == ""){
            layer.msg("请输入手机号",{time:2000,offset:300,icon:0});return;
        }
        if(!isMobile.test(mobile)){
            layer.msg("手机格式不正确",{time:2000,offset:300,icon:0});return;
        }
        send_flag = false;
        $.ajax({
            type: "POST",
            url: "/site/send_bmobile_code",
            async:true, 
            data: 'uMobile=' + mobile,
            success: function(resp) {
                resp = $.parseJSON(resp);
                if(resp.reqStatus){
                    _self.css("background","#dcdcdc");
                    return start_time();
                }else{
                    send_flag = true;
                    layer.msg(resp.errorMsg,{time:2000,offset:300,icon:0});return;
                    return;
                }
            }
        });
    }
    send();
}
var update_p = function(num,t){
    if (num == t) {
        send_flag = true;
        $("#send-code-btn").text("重新获取").css("background","#f07070");
    } else {
        var printnr = t - num;
        $("#send-code-btn").text("剩余"+printnr + "秒").css("background","#dcdcdc");
    }
}


